<?php
//include XML Header (as response will be in xml format)
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
         header("Content-type: application/xhtml+xml"); } else {
         header("Content-type: text/xml");
}


    session_start(); // muss zuerst aufgerufen werden, sonst gibt es fehlermeldungen
    echo '<?xml version="1.0" encoding="utf-8"?>';                       
    require "[system_path]system/framework.php";   // muss nacherher aufgerufen werden, da sonst Probleme mit Leerzeile 
                                                    // aber wegen session senden gibts ein problem

//start session to build different trees for different sessions (if you set $_SESSION["id"] to some hardcoded value, this way of processing will be skipped)
//session_start();
//if(!isset($_SESSION["id"]))
    //$_SESSION["id"] = microtime();

//include db connection settings
//require_once("file:///C|/xampp/common/config.php");
//require("../start.php");  
    

    //$table = "todo";
    $table = $_REQUEST['table'];
    //$where = $_REQUEST['where'];
    //$where = "titel='web'";
    $where = "";
    
    
    //$table = sqlGetDomainTable($table);

//$link = mysql_pconnect($mysql_host, $mysql_user, $mysql_pasw);
//$db = mysql_select_db ($mysql_db);


//FUNCTIONS TO USE IN THE CODE LATER

//print tree XML based on parent_id (function calls itself to go through the nested levels)
    function getLevelFromDB($parent_id){

        //$table = "cms";
        global $table;
        global $where;

    global $domain;
    global $domainModus;
    global $domainSql;
    
    //$domainSql = "";
    //if ($domainModus == "on") $domainSql = " AND domain = '".$domain."' ";



        //get tree level from database taking parent id as incomming argument               and GUID='".$_SESSION["id"]."'
        //$sql = "SELECT  id, titel FROM ".$table." WHERE (app = '' OR app IS NULL) AND item_parent_id=$parent_id $domainSql ORDER BY item_order";
           
       
        

        if ($where !== "") {
                $where=$where . " AND ";
        }
                
        $where = "item_parent_id='$parent_id'"; //" AND _usergroup_edit=30";           
                
        //$sql = "SELECT  id, titel FROM ".$table." WHERE $where ORDER BY item_order"; 
        $orderby = "item_order";
        
        
     /*   if ($parent_id === 0) {
                //$db = new db();
                //$webId = $db->getValue("SELECT id FROM page WHERE titel='web'");
                $sql = "SELECT  id, titel FROM ".$table." WHERE item_parent_id=0 AND titel='web' ORDER BY item_order"; 
        }*/
        //$webId = $db->getValue("SELECT id FROM page WHERE titel='web'");   
         
         
         foreach (db::getData($table, $where, $orderby) as $data) {
             
              $title = $data['title'];
                
                $title = str_replace('&',"",$title);
                //$titel = str_replace('ä',"",$titel);  
                //$titel = str_replace('&auml;',"",$titel);  
                
                
                // da Probleme mit Umlauten
                $title = utf8_decode($title);
                $title = convert::convertUmlaute($title);
                
                
                
                
                /*$title = str_replace("ü", "ue", $title);
                $title = str_replace("ä", "ae", $title);
                $title = str_replace("ö", "oe", $title);
                $title = str_replace("Ü", "ue", $title);
                $title = str_replace("Ä", "ae", $title);
                $title = str_replace("Ö", "Oe", $title); */
                
                
                
                
                
                
                echo '<item id="'.$data['id'].'" text="'. $title.'">';
                
                
                //include child nodes
                getLevelFromDB($data['id']);
                //close xml tag for the node
                echo "</item>\n";
             
             
             
         }
         
         
         
         
        
        
       /* $res = mysql_query ($sql);
        if($res){
            while($row=mysql_fetch_array($res)){
                //create xml tag for tree node
                //print("<item id='".$row['id']."' text=\"". str_replace('"',"&quot;",$row['titel'])."\">");
                
                $titel = $row['titel'];
                
                $titel = str_replace('&',"",$titel);
                //$titel = str_replace('ä',"",$titel);  
                //$titel = str_replace('&auml;',"",$titel);  
                
                
                
                print("<item id='".$row['id']."' text=\"". $titel."\">");
                
                
                //include child nodes
                getLevelFromDB($row['id']);
                //close xml tag for the node
                print("</item>\n");
            }
        }else{
            echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>".$sql;
        }*/    
    }

//XML HEADER

//include XML Header (as response will be in xml format)
/*if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
         header("Content-type: application/xhtml+xml"); } else {
         header("Content-type: text/xml");
}
echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); */              

    //print tree XML
    //getLevelFromDB(0);
    //checkLogin();
    //global $userGrp;
    
    
    //$db = new db();
    //$webId = $db->getValue("SELECT id FROM page WHERE titel='web'");
    
    //echo $webId;
      //<!-- start tree xml -->
    
    $startId = $_REQUEST['startid'];
    
    echo '<tree id="'.$startId.'">';  
    //echo '<tree id="0">';  
    
    //getLevelFromDB(0); 
    //getLevelFromDB(1); 
    getLevelFromDB($startId); 
    
    


    
    
    //getLevelFromDB(1);         
    
    /*if ($userGrp < 40) {
    
    getLevelFromDB(1);
    }
    else {
       getLevelFromDB(0);   
    }   */
    
    
    //Close db connection
//    mysql_close($link);
?>

<!-- close tree xml -->
</tree>
